<?php
#后台用户表
class AdminUserModel extends PdoModel {
    protected $_name = 'admin_user';

    function __construct(){
        parent::__construct();
    }

    #插入
    function addData($data){
        if(empty($data)){
            return false;
        }
        $data['created_at'] = time();
        return $res = $this->insert($data);
    }


    #更新
    function updateData($data = array(), $uid = 0){
        if($uid <= 0 || empty($data)){
            return false;
        }

        $data['updated_at'] = time();
        return $this->update($data, "uid = {$uid}");
    }


    #查找单条信息
    function getDataByUid($uid = 0){
        if (empty($uid)) {
            return false;
        }
        $sql = " select * from {$this->_name} where uid = {$uid} ";
        try {
            $data = $this->fetchRow($sql);
        } catch (Exception $ex) {
            $data = array();
        }
        return $data;
    }


    #登录
    function login($email = '',$password = ''){
        if(empty($email) || empty($password)){
            throw new Exception('邮箱或密码不能为空');
        }
        $where = "email='{$email}'";
        $user_info = $this->find($where);

        if(md5(md5($password).$user_info['salt']) == $user_info['password']){
            return $user_info;
        }else{
            return array();
        }
    }

    function getListData($page = 1, $page_size = 20, $condition = array()){
        $sql = " select * from  {$this->_name} where 1 ";

        if(!empty($condition['groupid'])){
            $sql .= " and groupid = '{$condition['groupid']}' ";
        }

        $sql .= " order by uid desc";

        $start = ($page - 1) * $page_size;
        $sql .= " limit {$start}, {$page_size}";
        try{
            $data = $this->fetchAll($sql);
        }catch(Exception $ex){
            $data = array();
        }
        return $data;
    }

    function getListCount($condition = array()){
        $sql = " select count(*) as num from  {$this->_name} where 1 ";

        if(!empty($condition['groupid'])){
            $sql .= " and groupid = '{$condition['groupid']}' ";
        }
        $result = $this->fetchRow($sql);
        $num = 0;
        if (!empty($result['num'])) {
            $num = $result['num'];
        }
        return $num;
    }
}
